package com.dev.toolkit.common.response;

import com.dev.toolkit.common.enums.ResultEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @date 2024/4/6 16:00
 * @description 统一响应结果类
 */
@Data
public class Result<T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;

    /**
     * 私有构造方法
     */
    private Result() {
    }

    /************************** common method **************************/
    /* 响应成功 */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMessage());
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure() {
        Result<Void> result = new Result<>();
        result.setCode(ResultEnum.FAILED.getCode());
        result.setMessage(ResultEnum.FAILED.getMessage());
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(String message) {
        Result<Void> result = new Result<>();
        result.setCode(ResultEnum.FAILED.getCode());
        result.setMessage(message);
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(int code, String message) {
        Result<Void> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(ResultEnum resultEnum) {
        Result<Void> result = new Result<>();
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMessage());
        return result;
    }

    /* 自定义响应状态码 */
    public Result<T> code(int code) {
        this.setCode(code);
        return this;
    }

    /* 自定义响应提示信息 */
    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    /* 自定义响应数据 */
    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }
}